package com.dx.esb.m8583.config;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.dx.esb.m8583.map.FiledBean;
import com.dx.esb.m8583.map.Map8583Bean;
import com.dx.esb.m8583.type.IsoType;

public class Config {
	private Map paseMeta=new HashMap();
	private Map headerMeta=new HashMap();
	
	public Map getPaseMeta() {
		return paseMeta;
	}

	public void setPaseMeta(Map paseMeta) {
		this.paseMeta = paseMeta;
	}
	
	public void parse(InputStream input) throws DocumentException{
		SAXReader reader = new SAXReader();
		Document document = reader.read(input);
		Element root = document.getRootElement();
		
		List<Element> headerElement = root.elements("header");
		if(headerElement != null && headerElement.size() > 0){
            for(Iterator it = headerElement.iterator();it.hasNext();){
                Element template = (Element)it.next();
                headerMeta.put(template.getText(), template.attribute("type").getText());
            }
        }
		
		
		List<Element> templates = root.elements("template");
		if(templates != null && templates.size() > 0){
            for(Iterator it = templates.iterator();it.hasNext();){
                Element template = (Element)it.next();
                listNodes(template);
            }
        }
	}
	

	public void parse() throws Exception{
		SAXReader reader = new SAXReader();
		File xml = new File("conf/config.xml");
		Document document = reader.read(xml);
		Element root = document.getRootElement();
		
		List<Element> headerElement = root.elements("header");
		if(headerElement != null && headerElement.size() > 0){
            for(Iterator it = headerElement.iterator();it.hasNext();){
                Element template = (Element)it.next();
                headerMeta.put(template.getText(), template.attribute("type").getText());
            }
        }
		
		
		List<Element> templates = root.elements("template");
		if(templates != null && templates.size() > 0){
            for(Iterator it = templates.iterator();it.hasNext();){
                Element template = (Element)it.next();
                listNodes(template);
            }
        }
	}
	
	public void listNodes(Element node) {  
		Map8583Bean mapbean=new Map8583Bean();
		
		
        //System.out.println("当前节点的名称：：" + node.getName());  
        
        List<Element> fields = node.elements("field");
        if(fields != null && fields.size() > 0){
            for(Iterator it = fields.iterator();it.hasNext();){
                Element field = (Element)it.next();
                FiledBean bean=new FiledBean();
                if(field.attribute("num")!=null){
                	bean.setDomain(Integer.parseInt(field.attribute("num").getText()));
                }
                if(field.attribute("length")!=null){
	                bean.setLength(Integer.parseInt(field.attribute("length").getText()));
                }
                if(field.attribute("type")!=null){
                	bean.setType(mappingValue(field.attribute("type").getText())) ;
                }
                bean.setInfo(field.getStringValue());
                if(field.attribute("bcd")!=null){
                	bean.setBcd(field.attribute("bcd").getText());
                }
                mapbean.setFiledBean(""+bean.getDomain(), bean);
                mapbean.getList().add(bean);
                
            }
        }
        this.paseMeta.put(node.attribute("type").getText(), mapbean);
        
    }  
	
	public IsoType mappingValue(String str){
		IsoType enumValue = null;
		if(str.equalsIgnoreCase("LLVAR")){
			enumValue = enumValue.LLVAR;
		}else if(str.equalsIgnoreCase("ALPHA")){
			enumValue = enumValue.ALPHA;
		}else if(str.equalsIgnoreCase("NUMERIC")){
			enumValue = enumValue.NUMERIC;
		}else if(str.equalsIgnoreCase("LLLVAR")){
			enumValue = enumValue.LLLVAR;
		}else if(str.equalsIgnoreCase("DATE10")){
			enumValue = enumValue.DATE10;
		}else if(str.equalsIgnoreCase("DATE4")){
			enumValue = enumValue.DATE4;
		}else if(str.equalsIgnoreCase("DATE_EXP")){
			enumValue = enumValue.DATE_EXP;
		}else if(str.equalsIgnoreCase("TIME")){
			enumValue = enumValue.TIME;
		}else if(str.equalsIgnoreCase("AMOUNT")){
			enumValue = enumValue.AMOUNT;
		}else if(str.equalsIgnoreCase("BINARY")){
			enumValue = enumValue.BINARY;
		}else if(str.equalsIgnoreCase("LLBIN")){
			enumValue = enumValue.LLBIN;
		}else if(str.equalsIgnoreCase("LLLBIN")){
			enumValue = enumValue.LLLBIN;
		}else if(str.equalsIgnoreCase("NFIXED")){
			enumValue = enumValue.NFIXED;
		}
		
		return enumValue;
	}
	
	public static void main(String[] args) throws Exception{
		Config config = new Config();
		config.parse();
		Map8583Bean bean = (Map8583Bean)config.getPaseMeta().get("0200");
		System.out.println(bean.getFiledBean("3").getInfo());
		
		
	}

	public Map getHeaderMeta() {
		return headerMeta;
	}

	public void setHeaderMeta(Map headerMeta) {
		this.headerMeta = headerMeta;
	}
}
